var subTaskId = JSON.parse($.cookie("subTaskId"));
// 切换示例图片显示隐藏
$("#showImage").on('click', function () {
    $("#samplePictures").slideToggle('fast');
    if ($("#fa").attr("class") == "fa fa-chevron-down") {
        $("#fa").attr("class", "fa fa-chevron-up");
    } else if ($("#fa").attr("class") == "fa fa-chevron-up") {
        $("#fa").attr("class", "fa fa-chevron-down");
    }
});
// 删除图片
var deleteimg = new Array();
var rejectImageIdList = new Array();

$("#pendingPictures").on('click', '.deleteimg', function () {
    if ($(this).css("background-image").indexOf("delete_2.png") > 0) {
        $(this).css("background-image", "url('./images/delete_1.png')");
        var imgId = $(this).parent().prev().find('img').attr('data-id'); // 获取不合格图片id
        rejectImageIdList.push(imgId);
        console.log(rejectImageIdList);
    } else if ($(this).css("background-image").indexOf("delete_1.png") > 0) {
        $(this).css("background-image", "url('./images/delete_2.png')");
        var imgId = $(this).parent().prev().find('img').attr('data-id'); // 获取撤销图片id
        for (var i = 0, len = rejectImageIdList.length; i < len; i++) {
            if (rejectImageIdList[i] == imgId) {
                rejectImageIdList.splice(i, 1);
                break;
            }
        }
        console.log(rejectImageIdList);
    }
});
// 查看图片时将不合格的图片在图片查看器里做标识
$("#reviewImagesList").on('click', '.imgs', function () {
    setTimeout(function () {
        for (var i = 0, len = $("#reviewImagesList").children("li").length; i < len; i++) {
            var index = $("body").find(".viewer-list").find("li").eq(i).find('img').attr('data-id');
            for (var j = 0, lens = rejectImageIdList.length; j < lens; j++) {
                if (rejectImageIdList[j] == index) {
                    $("body").find(".viewer-list").find("li").eq(i).find('i').addClass("fa-del");
                }
            }
        }
    }, 500)
})
// 查看大图时选择删除图片
$("body").on('click', '.deleteId', function () {
    var index = $("body").find(".viewer-active").find('img').attr('data-id');
    console.log($("#reviewImagesList").children("li").length);
    for (var i = 0, len = $("#reviewImagesList").children("li").length; i < len; i++) {
        var imgId = $('#reviewImagesList').find("li").eq(i).find('.imgs').find('img').attr('data-id');
        if (imgId == index) {
            if ($.inArray(imgId, rejectImageIdList) == -1) {
                $('#reviewImagesList').find("li").eq(i).find('.delete').find('span').css("background-image", "url('./images/delete_1.png')");
                $("body").find(".viewer-active").find('i').addClass("fa-del");
                rejectImageIdList.push(imgId);
            } else if ($.inArray(imgId, rejectImageIdList) > -1) {
                $('#reviewImagesList').find("li").eq(i).find('.delete').find('span').css("background-image", "url('./images/delete_2.png')");
                $("body").find(".viewer-active").find('i').removeClass("fa-del");
                for (var i = 0, len = rejectImageIdList.length; i < len; i++) {
                    if (rejectImageIdList[i] == imgId) {
                        rejectImageIdList.splice(i, 1);
                        break;
                    }
                }
            }
        }
    }
    console.log(rejectImageIdList);
});

// 获取子任务数据
var subTaskData;
$.ajax({
    url: contextPath + "/subtask/" + subTaskId,
    type: 'GET',
    dataType: "json",
    contentType: "application/json",
    success: function (data) {
        if (data.code == 1) {
            console.log(data);
            subTaskData = data.data;
            var deviceTypeId = data.data.deviceTypeId;
            var workOrderTypeId = data.data.workOrderTypeId;
            var projectId = data.data.projectId;
            // 查询流程规则
            $.ajax({
                url: contextPath + "/ProcessRule/queryProcessRuleByDeviceTypeOrderTypeProject",
                type: 'GET',
                data: {"deviceTypeId": deviceTypeId, "workOrderTypeId": workOrderTypeId, "projectId": projectId},
                dataType: 'json',
                contentType: "application/json",
                success: function (data) {
                    if (data.code == 1) {
                        if (data.data == null) {
                            toastr.error("流程规则异常！");
                        } else {
                            console.log(data.data);
                            // 示例图
                            var str = '';
                            for (var i = 0, len = data.data.processRuleToImageFileList.length; i < len; i++) {
                                str += '<li>' +
                                    '<img data-original="' + data.data.processRuleToImageFileList[i].path + '" src="' + data.data.processRuleToImageFileList[i].downPath + '" data-id="" alt="' + data.data.processRuleToImageFileList[i].description + '">' +
                                    '<div class="imgDescription">' + data.data.processRuleToImageFileList[i].description + '</div>' +
                                    '</li>';
                            }
                            $("#samplePictureList").html(str);
                            subTaskData.processRule = data.data;

                            // 审核图片
                            var str = '';
                            for (var i = 0, len = subTaskData.subTaskImageList.length; i < len; i++) {
                                str += '<li>' +
                                    '<div class="imgs">' +
                                    '<img data-original="' + subTaskData.subTaskImageList[i].imagePath + '" src="' + subTaskData.subTaskImageList[i].imagePath + '" data-id="' + subTaskData.subTaskImageList[i].subImageId + '" alt="' + getImageDescription(subTaskData, i) + '">' +
                                    '<div class="imgDescription">' + getImageDescription(subTaskData, i) + '</div>' +
                                    '</div>' +
                                    '<div class="delete">' +
                                    '<span class="deleteimg"></span>' +
                                    '</div>' +
                                    '</li>';
                            }
                            $("#reviewImagesList").html(str);
                            // 流程信息
                            $("#VivaTimeline").html(template('taskProcessInfoTpl', subTaskData));
                            $('.VivaTimeline').vivaTimeline({
                                carousel: false
                            });

                            $("#basicInformation").html(template('checkBasicInformationTpl', subTaskData));
                            console.log(subTaskData.processRule.additionalBusinessId);
                            // 示例图片查看
                            $('#samplePictureList').viewer({
                                deleteId: false
                            });
                            // 审核图片查看
                            $('#reviewImagesList').viewer();
                        }
                    }
                },
                error: function (data) {
                    toastr.error(data.message, "请求失败");
                }
            });
        }
    },
    error: function (data) {
        toastr.error(data.message, "请求失败");
    }
});

function getImageDescription(subTask, index) {
    var description = '';

    if (subTask != null) {
        if (index < subTask.processRule.processRuleToImageFileList.length) {
            description = subTask.processRule.processRuleToImageFileList[index].description
        } else {
            var k = index - subTask.processRule.processRuleToImageFileList.length + 1;
            description = subTask.deviceId + '_' + k;
        }
    }

    return description;
}

// 审核
// 通过
$("#pass").on('click', function () {
    $.ajax({
        url: contextPath + "/subtask/check",
        type: 'POST',
        data: JSON.stringify({"accred": true, "description": '', "rejectImageIdList": [], "subTaskId": subTaskId}),
        dataType: 'json',
        contentType: "application/json",
        success: function (data) {
            if (data.code == 1) {
                toastr.success('操作成功！');
                location.hash = "qualitycontrol-assignmentGroup-pending";
                console.log(data);
            }else{
                toastr.error(data.message, "操作失败");
            }
        },
        error: function (data) {
            toastr.error(data.message, "请求失败");
        }
    });
});
// 驳回
$("#reject").on('click', function () {
    var description = $("#rejectReason").val();
    if ($.trim(description) == '') {
        toastr.error('请填写驳回理由！');
        return;
    }
    $.ajax({
        url: contextPath + "/subtask/check",
        type: 'POST',
        data: JSON.stringify({
            "accred": false,
            "description": description,
            "rejectImageIdList": rejectImageIdList,
            "subTaskId": subTaskId
        }),
        dataType: 'json',
        contentType: "application/json",
        success: function (data) {
            if (data.code == 1) {
                $('#myModal').modal('hide');
                toastr.success('操作成功！');
                setTimeout(function () {
                    location.hash = "qualitycontrol-assignmentGroup-pending";
                }, 1000);
                console.log(data);
            }else{
                toastr.error(data.message, "操作失败");
            }
        },
        error: function (data) {
            toastr.error(data.message, "请求失败");
        }
    });
});